package cn.redragon.soa.data.criteria;


import cn.redragon.soa.common.criteria.SearchCriteria;
import cn.redragon.soa.common.enums.StockActionType;
import cn.redragon.soa.data.entity.QStockHistoryEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import java.time.LocalDateTime;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class StockHistoryCriteria extends SearchCriteria {

    private String sku;
    private String spu;
    private String type;
    @JsonIgnore
    private String brand;
    private String orderNumber;
    private StockActionType action;
    private LocalDateTime startTime;
    private LocalDateTime endTime;

    public Predicate toPredicate() {
        QStockHistoryEntity entity = QStockHistoryEntity.stockHistoryEntity;
        Predicate predicate = entity.isNotNull();

        predicate = ExpressionUtils.and(predicate, entity.brand.eq(brand));
        if (type != null) {
            predicate = ExpressionUtils.and(predicate, entity.type.eq(type));
        }
        if (sku != null) {
            predicate = ExpressionUtils.and(predicate, entity.sku.eq(sku));
        }
        if (spu != null) {
            predicate = ExpressionUtils.and(predicate, entity.spu.eq(spu));
        }
        if (orderNumber != null) {
            predicate = ExpressionUtils.and(predicate, entity.orderNumber.eq(orderNumber));
        }
        if (action != null) {
            predicate = ExpressionUtils.and(predicate, entity.action.eq(action));
        }
        if (startTime != null) {
            predicate = ExpressionUtils.and(predicate, entity.createTime.goe(startTime));
        }
        if (endTime != null) {
            predicate = ExpressionUtils.and(predicate, entity.createTime.loe(endTime));
        }

        return predicate;
    }

}
